

/*
Copyright 2008, 2009, 2010 Brightworks, Inc.

This file is part of Language Mentor.

Language Mentor is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Language Mentor is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Language Mentor.  If not, see <http://www.gnu.org/licenses/>.
*/
package com.langcollab.languagementor.view
{
    import com.brightworks.util.Debug;
    import com.brightworks.util.Utils_String;
    import com.langcollab.languagementor.constant.Constant_Misc;
    import com.langcollab.languagementor.event.Event_ApplicationState;
    import com.langcollab.languagementor.model.DataManager;
    import com.langcollab.languagementor.model.MainModel;
    import com.langcollab.languagementor.vo.LearningModeVO;

    import flash.events.MouseEvent;
    import flash.system.Capabilities;
    import flash.utils.Dictionary;

    import flashx.textLayout.formats.TextAlign;

    import mx.binding.utils.ChangeWatcher;
    import mx.collections.ArrayCollection;
    import mx.events.FlexEvent;
    import mx.events.PropertyChangeEvent;
    import mx.events.ResizeEvent;

    import spark.components.Button;
    import spark.components.Group;
    import spark.components.Label;
    import spark.components.List;
    import spark.components.TextArea;
    import spark.components.ToggleButton;
    import spark.components.VGroup;
    import spark.components.supportClasses.StyleableTextField;
    import spark.events.IndexChangeEvent;
    import spark.layouts.VerticalLayout;

    public class View_SelectMode extends View_Base 
    {
        [Inject("mainModel.dataManager")]
        public var dataManager:DataManager;
        [Inject]
        public var model:MainModel;

        private var _helpTextField:TextArea;
        private var _index_ButtonToLearningModeVO:Dictionary;
        private var _mainGroup:Group;
        private var _modeList:List;
        private var _modeListDataProvider:ArrayCollection;
        private var _selectedLearningModeId:int;
        private var _uiCreated:Boolean = false;

        // ****************************************************
        //
        //          Public Methods
        //
        // ****************************************************

        public function View_SelectMode()
        {
            super();
            title = "Select Mode";
            addEventListener(ResizeEvent.RESIZE, onResize);
        }

        override public function onBackButtonPress():void
        {
            var e:Event_ApplicationState = new Event_ApplicationState(Event_ApplicationState.CHANGE_CURRENT_LEARNING_MODE);
            e.newLearningModeId = _selectedLearningModeId;
            dispatchEvent(e);
            navigator.popView(transition_SlideView_Left);
        }

        [PostConstruct]
        public function onPostConstruct():void
        {
            ChangeWatcher.watch(model, "dataManagerInitialized", onUserDataInitialized);
        }

        // ****************************************************
        //
        //          Protected Methods
        //
        // ****************************************************

        override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
        {
            super.updateDisplayList(unscaledWidth, unscaledHeight);
            if (model.dataManagerInitialized)
            {
                // We make sure that we only do this once. The act of creation 
                // invalidates the display list, so we'll get an endless loop if we don't.
                if (!_uiCreated)
                {
                    createUI();
                }
                _helpTextField.height = (this.height - _modeList.height) - 10;
                if (_modeList.layout)
                {
                    VerticalLayout(_modeList.layout).variableRowHeight = false;
                    VerticalLayout(_modeList.layout).rowHeight = 50;
                }
            }
        }

        // ****************************************************
        //
        //          Private Methods
        //
        // ****************************************************

        private function createUI():void
        {
            _index_ButtonToLearningModeVO = new Dictionary();
            _modeListDataProvider = new ArrayCollection;            
            //// change this layout when screen orientation changes
            _mainGroup = new VGroup();
            _mainGroup.percentHeight = 100;
            _mainGroup.percentWidth = 100;
            addElement(_mainGroup);
            _modeList = new List();
            _modeList.percentWidth = 100;
            _modeList.dataProvider = _modeListDataProvider;
            _modeList.addEventListener(IndexChangeEvent.CHANGE, onModeListChange);
            _mainGroup.addElement(_modeList);
            _helpTextField = new TextArea();
            _helpTextField.percentWidth = 100;
            _helpTextField.selectable = false;
            _mainGroup.addElement(_helpTextField);
            var learningModeIds:Array = dataManager.getLearningModeIDListSortedByLocationInOrder();
            for each (var learningModeId:int in learningModeIds)
            {
                var learningModeVO:LearningModeVO = dataManager.getLearningModeVOFromID(learningModeId);
                var modeLabelToken:String = learningModeVO.labelToken;
                var modeLabel:String = dataManager.getNativeLanguageResource("label_LearningMode_" + modeLabelToken);
                modeLabel = Utils_String.replaceAll(modeLabel, Constant_Misc.TOKEN_NATIVE_LANGUAGE_NAME, dataManager.getLanguageNameTranslation_ForNativeLanguage_InNativeLanguage());
                modeLabel = Utils_String.replaceAll(modeLabel, Constant_Misc.TOKEN_TARGET_LANGUAGE_NAME, dataManager.getLanguageNameTranslation_ForTargetLanguage_InNativeLanguage());
                var item:Object = {label:modeLabel, value:learningModeId};
                _modeListDataProvider.addItem(item);
            }
            _uiCreated = true;
        }

        private function displayLearningModeButtonHelpText(modeId:uint):void
        {
            var htmlString:String = model.learningModeDescriptionsHTML[modeId];
            htmlString = Utils_String.removeWhiteSpaceAtBeginningsOfLines(htmlString);
            htmlString = Utils_String.removeLineBreaks(htmlString);
            htmlString = Utils_String.replaceAll(htmlString,  Constant_Misc.TOKEN_NATIVE_LANGUAGE_NAME,     dataManager.getLanguageNameTranslation_ForNativeLanguage_InNativeLanguage()  );
            htmlString = Utils_String.replaceAll(htmlString,  Constant_Misc.TOKEN_TARGET_LANGUAGE_NAME,     dataManager.getLanguageNameTranslation_ForTargetLanguage_InNativeLanguage()  );
            htmlString = Utils_String.replaceAll(htmlString,  Constant_Misc.TOKEN_NATIVE_LANGUAGE_EXAMPLE,  dataManager.getNativeLanguageResource("languageExample") );
            htmlString = Utils_String.replaceAll(htmlString,  Constant_Misc.TOKEN_TARGET_LANGUAGE_EXAMPLE,  dataManager.getTargetLanguageResource("languageExample") );
            // See http://help.adobe.com/en_US/flex/mobileapps/WS19f279b149e7481c-3c2dbaa612bb06a5ea2-8000.html
            StyleableTextField(_helpTextField.textDisplay).htmlText = htmlString;
        }

        private function onModeListChange(event:IndexChangeEvent):void
        {
            var modeId:uint = Object(_modeList.selectedItem).value;
            displayLearningModeButtonHelpText(modeId);
            _selectedLearningModeId = modeId;
        }

        private function onResize(event:ResizeEvent):void
        {

        }

        private function onUserDataInitialized(e:PropertyChangeEvent):void
        {
            this.invalidateDisplayList();
        }

    }
}

